﻿<UserControl x:Class="BadHabit.Factory.Controls.BacklogDataGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    xmlns:Microsoft_Toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:BadHabit_Factory="clr-namespace:BadHabit.Factory"             
    xmlns:BadHabit_WPFTools="clr-namespace:BadHabit.WPFTools;assembly=BadHabit.WPFTools"
    xmlns:BadHabit_WPFTools_Converters="clr-namespace:BadHabit.WPFTools.Converters;assembly=BadHabit.WPFTools"
    xmlns:BadHabit_Factory_Converters="clr-namespace:BadHabit.Factory.Converters"  
    xmlns:Factory_Core="clr-namespace:BadHabit.Factory;assembly=FactoryCore"
    xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"             

    xmlns:props="clr-namespace:BadHabit.Factory.Properties"
             
             

    x:Name="control"
    DataContextChanged="UserControl_DataContextChanged">
    <UserControl.Resources>

        
        
        <BadHabit_Factory_Converters:VelocityIsHigherConverter x:Key="VelocityIsHigherConverter"/>
        <BadHabit_Factory_Converters:SprintBySprintUIdConverter x:Key="SprintBySprintUIdConverter"/>

       
        <Style x:Key="GroupContainerStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="Local"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander x:Name="expander"
                                  Margin="0"  
                                  Padding="0"          
                                  IsExpanded="True"
                                  HorizontalAlignment="Stretch"
                                  IsTabStop="False" Focusable="False"
                                  Loaded="Expander_Loaded"
                                  DataContext="{Binding Name, Converter={StaticResource SprintBySprintUIdConverter}}">                         
                                <Expander.Header>
                                <Border Background="Transparent" BorderBrush="LightGray" BorderThickness="0,0,0,0" Padding="3">                                    
                                    <TextBlock Text="{Binding SprintNumber, StringFormat='{}Sprint {0}'}" Foreground="#0050B0" FontSize="13">
                                        <Image x:Name="sprintImage"
                                               Margin="10,0,0,0"
                                           Source="/Images/Icons/sprint.png"                                               
                                           ToolTip="This is the current sprint" Width="10" Height="10" Stretch="Uniform">
                                        <Image.Style>
                                            <Style>
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsCurrentSprint}" Value="True">
                                                        <Setter Property="Image.Visibility" Value="Visible"/>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding IsCurrentSprint}" Value="False">
                                                        <Setter Property="Image.Visibility" Value="Hidden"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Image.Style>
                                    </Image>
                                    </TextBlock>
                                </Border>
                                    
                            
                            </Expander.Header>
                            
                      
                            <!--<StackPanel BadHabit_WPFTools:RoleChecker.RequiredEnableRole="SCRUM_MASTER">-->
                            <StackPanel >
                                
                                    
                                <ItemsPresenter  />
                                
                                <Grid Margin="40,4,0,4">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>

                                    <StackPanel Grid.Column="0" Orientation="Horizontal">
                                        <TextBlock Text="{x:Static props:Resources.Starts_at}"/>
                                        <Microsoft_Toolkit:DatePicker  
                                            BadHabit_WPFTools:RoleChecker.RequiredEnableRole="SCRUM_MASTER"
                                            IsTabStop="False"                                         
                                            DataContext="{Binding ElementName=control, Path=DataContext}"
                                            Margin="5,0,0,0"                                            
                                            SelectedDate="{Binding Project[0].EstimatedStartDate, ValidatesOnExceptions=True}"/>                                            
                                        <StackPanel.Style>
                                            <Style>
                                                <Setter Property="Grid.Visibility" Value="Collapsed"/>
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding SprintNumber}" Value="1">
                                                        <Setter Property="Grid.Visibility" Value="Visible"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </StackPanel.Style>
                                    </StackPanel>


                                    <TextBlock Grid.Column="1" Text="{x:Static props:Resources.Ends_at}"/>

                                    <Microsoft_Toolkit:DatePicker                                 
                                        BadHabit_WPFTools:RoleChecker.RequiredEnableRole="SCRUM_MASTER"
                                        IsTabStop="False"                                                                                 
                                        Margin="5,0,0,0"
                                            Grid.Column="2"
                                            SelectedDate="{Binding ExpirationDate, ValidatesOnExceptions=True}">
                                        <Microsoft_Toolkit:DatePicker.Style>
                                            <Style TargetType="Microsoft_Toolkit:DatePicker" BasedOn="{StaticResource {x:Type Microsoft_Toolkit:DatePicker}}">
                                                <Style.Resources>
                                                    <Style TargetType="Microsoft_Toolkit:DatePickerTextBox" BasedOn="{StaticResource FormTextBox}">
                                                        <Setter Property="VerticalAlignment" Value="Center"/>
                                                        <Setter Property="TextAlignment" Value="Right"/>
                                                        <Setter Property="MinWidth" Value="70"/>
                                                        <Setter Property="IsTabStop" Value="False"/>
                                                    </Style>
                                                </Style.Resources>
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsPlanOver}" Value="True">
                                                        <Setter Property="IsEnabled" Value="False"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Microsoft_Toolkit:DatePicker.Style>
                                    </Microsoft_Toolkit:DatePicker>
                                    
                                    <TextBlock Grid.Column="3" Margin="10,0,0,0">
                                        <Hyperlink Command="BadHabit_Factory:FactoryCommands.BreakSprint"                                                    
                                                   ToolTip="{x:Static props:Resources.Break_sprint_tooltip}">
                                            <TextBlock Text="{x:Static props:Resources.Break_this_sprint}"/>
                                            </Hyperlink>
                                        <TextBlock.Style>
                                            <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}" >
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsPlanOver}" Value="True">
                                                            <Setter Property="TextBlock.IsEnabled" Value="False"/>
                                                        </DataTrigger>               
                                                        <DataTrigger Binding="{Binding IsLastSprint}" Value="False">
                                                            <Setter Property="TextBlock.Visibility" Value="Collapsed"/>
                                                        </DataTrigger>                                                        
                                                        
                                                    </Style.Triggers>
                                                </Style>
                                        </TextBlock.Style>
                                    </TextBlock>
                                    
                                    <TextBlock Grid.Column="4"
                                               HorizontalAlignment="Right"
                                               FontStyle="Italic"
                                               Text="{Binding CodeVersion, StringFormat={x:Static props:Resources.Revision_N}}" Foreground="Gray"
                                               Margin="0,0,10,0">
                                            <TextBlock.Style>
                                                <Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="TextBlock">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding CodeVersion}" Value="0">
                                                            <Setter Property="TextBlock.Visibility" Value="Collapsed"/>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </TextBlock.Style>
                                    </TextBlock>
                                    
                                    

                                    <Image
                                            Margin="0,0,5,0"
                                            Grid.Column="5"
                                            VerticalAlignment="Center"
                                            Source="/Images/Icons/teamMemberMeter.png"
                                            Stretch="None"
                                            Height="16"
                                            MaxWidth="128"
                                            Width="{Binding NumberOfMembersNeeded, Converter={StaticResource MultiplyConverter}, ConverterParameter=16}">
                                        <Image.ToolTip>                                            
                                            <TextBlock Text="{Binding NumberOfMembersNeeded, StringFormat={x:Static props:Resources.You_will_need_N_guys_to_perform_this_sprint}}"/>
                                        </Image.ToolTip>
                                    </Image>
                                    

                                    <TextBlock Grid.Column="6" Text="{Binding TotalSize, StringFormat={x:Static props:Resources.N_pts_}}"  FontStyle="Italic" Foreground="Gray"/>
                                    
                                    <TextBlock Grid.Column="7" Margin="5,0,0,0" Text="{Binding TotalHours, StringFormat={x:Static props:Resources.N_hrs_}}" FontStyle="Italic" Foreground="Gray"/>
                                                                        
                                    <TextBlock Grid.Column="8" Margin="5,0,0,0" Text="{Binding WorkDayCount, StringFormat={x:Static props:Resources.N_days}}" Foreground="Gray"/>

                                    


                                </Grid>
                            </StackPanel>
                                
                            
                            
                        </Expander>                              
                        
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


        


    </UserControl.Resources>

    <UserControl.CommandBindings>
        <CommandBinding Command="BadHabit_Factory:FactoryCommands.DeleteBacklogItem" CanExecute="DeleteBacklogItem_CanExecute" Executed="DeleteBacklogItem_Executed"/>
        <CommandBinding Command="BadHabit_Factory:FactoryCommands.MoveUpClick" Executed="MoveUpClick_Executed" CanExecute="MoveUpClick_CanExecute"/>
        <CommandBinding Command="BadHabit_Factory:FactoryCommands.MoveDownClick" Executed="MoveDownClick_Executed" CanExecute="MoveDownClick_CanExecute"/>
        <CommandBinding Command="BadHabit_Factory:FactoryCommands.OpenItemDetail" Executed="OpenItemDetail_Executed" />
        <CommandBinding Command="BadHabit_Factory:FactoryCommands.BreakSprint" Executed="BreakSprint_Executed"  />        
    </UserControl.CommandBindings>

    <Grid>
        <BadHabit_WPFTools:DataGrid
                x:Name="backlogDataGrid"            
                
                
                BadHabit_WPFTools:RoleChecker.RequiredEditRole="SCRUM_MASTER"
            
                AutoGenerateColumns="False"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                
                IsSynchronizedWithCurrentItem="True"
            
                GridLinesVisibility="Horizontal"
           
                
                PreviewKeyDown="backlogDataGrid_PreviewKeyDown"                
               
                PreviewMouseDoubleClick="backlogDataGrid_MouseDoubleClick"
            
                AllowDrop="True"
                PreviewDragEnter="ReorderableListBox_PreviewDragEnter"
                PreviewDragLeave="ReorderableListBox_PreviewDragLeave"
                PreviewDragOver="ReorderableListBox_PreviewDragOver"
                PreviewDrop="ReorderableListBox_PreviewDrop"
                PreviewMouseLeftButtonUp="ReorderableListBox_PreviewMouseLeftButtonUp"
            
                
               
                UseNavigationExtensions="True" 
                    
                PreviewMouseLeftButtonDown="ReorderableListBox_PreviewMouseLeftButtonDown"
                
                PreviewMouseMove="ReorderableListBox_PreviewMouseMove"
            
                SelectionMode="Extended"
            
                                        
                CanUserDeleteRows="False"
                CanUserAddRows="False"
                CanUserSortColumns="False"
                CanUserReorderColumns="False"                        
                        
                ScrollViewer.VerticalScrollBarVisibility="Auto"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <Microsoft_Toolkit:DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="{x:Static props:Resources.Details}" Click="detailsMenuItem_Click"/>
                    <MenuItem Header="{x:Static props:Resources.Remove_item}" Command="BadHabit_Factory:FactoryCommands.DeleteBacklogItem"/>
                </ContextMenu>
            </Microsoft_Toolkit:DataGrid.ContextMenu>

            <Microsoft_Toolkit:DataGrid.Resources>

                <Style x:Key="BacklogRightAlignCellStyle" TargetType="Microsoft_Toolkit:DataGridCell" BasedOn="{StaticResource RightAlignBoldCellStyle}" >
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsPlanOver}" Value="True">
                            <Setter Property="Focusable" Value="False"/>
                            <Setter Property="FontWeight" Value="Regular"/>
                            <Setter Property="Foreground" Value="Gray"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
                <Style x:Key="BacklogLefttAlignCellStyle" TargetType="Microsoft_Toolkit:DataGridCell" BasedOn="{StaticResource DetailCellStyle}" >
                    <Setter Property="FontSize" Value="{StaticResource NeedToSeeFontSize}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsPlanOver}" Value="True">
                            <Setter Property="Focusable" Value="False"/>
                            <Setter Property="Foreground" Value="Gray"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Microsoft_Toolkit:DataGrid.Resources>





            <Microsoft_Toolkit:DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupContainerStyle}"   />
            </Microsoft_Toolkit:DataGrid.GroupStyle>

            <Microsoft_Toolkit:DataGrid.Columns>

                <Microsoft_Toolkit:DataGridTextColumn                                                
                        CanUserResize="False" IsReadOnly="True"
                        Binding="{Binding BacklogItemNumber}"       
                        CellStyle="{StaticResource MoveUpDownCellStyle}"
                        
                        Width="Auto"/>


                <Microsoft_Toolkit:DataGridTemplateColumn
                    CanUserResize="False"              
                    
                    IsReadOnly="True"
                    
                    Width="Auto">
                    <Microsoft_Toolkit:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,0"  >                                
                                <Image DataContext="{Binding Status}"
                               Style="{StaticResource BacklogItemStatusImage}" Width="16" Height="16" Margin="2" />
                                <Image Style="{StaticResource VerifiedImage}" Width="16" Height="16" Margin="2" />
                                <Image Style="{StaticResource ValidatedImage}" Width="16" Height="16" Margin="2" />
                            </StackPanel>

                        </DataTemplate>
                    </Microsoft_Toolkit:DataGridTemplateColumn.CellTemplate>
                </Microsoft_Toolkit:DataGridTemplateColumn>




                <Microsoft_Toolkit:DataGridTextColumn      
                        x:Name="titleColumn"
                        Width="*"
                        CanUserResize="True" 
                        
                        CellStyle="{StaticResource BacklogLefttAlignCellStyle}"                                                   
                        ElementStyle="{StaticResource NeedToSeeTextElementStyle}"
                        Header="{x:Static props:Resources.Item}"
                        Binding="{Binding Title}">
                    <Microsoft_Toolkit:DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox" BasedOn="{StaticResource NeedToSeeTextEditingElementStyle}">
                            <Setter Property="MaxLength" Value="150"/>
                        </Style>
                    </Microsoft_Toolkit:DataGridTextColumn.EditingElementStyle>
                </Microsoft_Toolkit:DataGridTextColumn>


                <Microsoft_Toolkit:DataGridTemplateColumn
                        Header="{x:Static props:Resources.Size}"
                        CanUserResize="True"                         
                        CellStyle="{StaticResource RightAlignCellStyle}"
                        Width="Auto">

                    <Microsoft_Toolkit:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <BadHabit_WPFTools:ComboBox                                               
                                        MinWidth="50"                                 
                                        Style="{StaticResource SizeClickOnComboBox}"                                                                        
                                        HorizontalAlignment="Right"
                                        HorizontalContentAlignment="Right"                                     
                                        VerticalAlignment="Center"                                                                        
                                        SelectionChanged="SizeComboBox_SelectionChanged"                                                                   
                                        ItemsSource="{Binding Source={StaticResource BacklogItemSizeList}}">
                                <BadHabit_WPFTools:ComboBox.GroupStyle>
                                    <GroupStyle HeaderTemplate="{StaticResource ComboBoxGroupHeaderTemplate}"/>
                                </BadHabit_WPFTools:ComboBox.GroupStyle>
                            </BadHabit_WPFTools:ComboBox>
                        </DataTemplate>
                    </Microsoft_Toolkit:DataGridTemplateColumn.CellEditingTemplate>
                    <Microsoft_Toolkit:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Size}" Margin="2"/>
                        </DataTemplate>
                    </Microsoft_Toolkit:DataGridTemplateColumn.CellTemplate>
                </Microsoft_Toolkit:DataGridTemplateColumn>


            </Microsoft_Toolkit:DataGrid.Columns>

        </BadHabit_WPFTools:DataGrid>    
        
    </Grid>

  

    

    
</UserControl>
